package org.example.classtopia.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.classtopia.Service.ResourceLikeService;
import org.example.classtopia.Common.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "资源点赞", description = "资源点赞相关接口")
@RestController
@RequestMapping("/resources/{resourceId}/likes")
public class ResourceLikeController {
    @Autowired
    private ResourceLikeService resourceLikeService;

    @Operation(summary = "点赞资源", description = "用户对指定资源进行点赞")
    @PostMapping
    public Result<Boolean> likeResource(@PathVariable Long resourceId, @RequestParam Long userId) {
        boolean result = resourceLikeService.likeResource(userId, resourceId);
        return Result.success(result);
    }

    @Operation(summary = "取消点赞", description = "用户取消对指定资源的点赞")
    @DeleteMapping
    public Result<Boolean> unlikeResource(@PathVariable Long resourceId, @RequestParam Long userId) {
        boolean result = resourceLikeService.unlikeResource(userId, resourceId);
        return Result.success(result);
    }

    @Operation(summary = "是否已点赞", description = "查询用户是否已点赞该资源")
    @GetMapping("/has-liked")
    public Result<Boolean> hasLiked(@PathVariable Long resourceId, @RequestParam Long userId) {
        boolean result = resourceLikeService.hasLiked(userId, resourceId);
        return Result.success(result);
    }

    @Operation(summary = "点赞数统计", description = "查询资源的点赞总数")
    @GetMapping("/count")
    public Result<Integer> countLikes(@PathVariable Long resourceId) {
        int count = resourceLikeService.countLikes(resourceId);
        return Result.success(count);
    }
}
